<?php

namespace app\admin\controller;

use app\_interface\ResourceInterface;
use app\common\entity\ResponseData;
use Sffi\ElementPlus\ElForm;
use Sffi\ElementPlus\ElForm\ElInput;
use Sffi\ElementPlus\ElForm\ElSelect;
use Sffi\ElementPlus\ElForm\ElSwitch;
use Sffi\ElementPlus\ElList;
use Sffi\ElementPlus\ElRule\Law;
use Sffi\ElementPlus\ElRule\Rule;
use think\facade\Request;

class User extends \app\admin\controller\AdminBase implements ResourceInterface
{

    public function list()
    {
        $where = input('where');
        $userModel = \app\model\User::when(isset($where['status']), fn($query) => $query->scope('status', $where['status']))
        ->when(isset($where['username']), fn($query) => $query->whereLike('username', '%'.$where['username'].'%'));
        $count = $userModel->count();
        $user = $userModel->select();
        return ResponseData::success($user, ['sum' => $count, 'page' => input('page', 1), 'size' => input('size', 10)]);
    }

    public function detail()
    {
        $id = input('id');
        $user = \app\model\User::with(['role'])->find($id);
        if ($user->role){
            $user['role_id'] = $user->role->id;
            $user['role_name'] = $user->role->name;
        }else{
            $user['role_id'] = 0;
            $user['role_name'] = '无角色';
        }
        return ResponseData::success($user);
    }

    public function add()
    {
        $data = Request::only(['username', 'password', 'status']);
        $user = \app\model\User::where(['username'=>$data['username']])->find();
        if (!$user){
            $user = \app\model\User::create($data);
            return ResponseData::success($user);
        }
        return ResponseData::error('该用户已存在');
    }

    public function update()
    {
        $data = Request::only(['username', 'password', 'status']);
        $user = \app\model\User::where(['username'=>$data['username']])->find();
        if (!$user || $user->id != input('id')){
            $user = \app\model\User::update($data, ['id'=>input('id')]);
            return ResponseData::success($user);
        }
        return ResponseData::error('该用户已存在');
    }

    public function delete($id)
    {
        $id = input('id');
        if ($id == 1){
            return ResponseData::error('无法删除超级管理员');
        }
        \app\model\User::destroy($id);
        return ResponseData::success(1);
    }

    public function getListColumns()
    {
        $list = new ElList();
        $statusColumn = new ElList\ElSwitch('状态', 'status', 'status', 1, 0);
        $statusColumn->setWidth(150);
        $list->addColumn(new ElList\ElInput('用户账号', 'username', 'username'))
            ->addColumn(new ElList\Column('创建时间', 'create_time', 'create_time'))
            ->addColumn(new ElList\Column('更新时间', 'update_time', 'update_time'))
            ->addColumn($statusColumn);
        $list->addSearch(new \Sffi\ElementPlus\ElSearch\ElInput('用户账号', 'username'))
            ->addSearch((new \Sffi\ElementPlus\ElSearch\ElSwitch('状态', 'status', 1, 0, '有效', '无效'))->setDefault(1));
        return ResponseData::success($list->__toArray());
    }

    public function getFormColumns()
    {
        $roles = \app\model\Role::select();
        $form = new ElForm();
        $roleColumn = new ElSelect('角色', 'role_id', '请选择用户角色');
        $roleColumn->addOptionByArgs('无角色', 0);
        foreach ($roles as $role) {
            $roleColumn->addOptionByArgs($role['name'], $role['id'], !$role['status']);
        }
        $roleColumn->setRemoteUrl('/admin/role/list','name','id');
        $form->addColumn(new ElInput('账号', 'username', '请输入用户账号'));
        $form->addColumn(new ElInput('密码', 'password', '请输入用户密码'));
        $form->addColumn($roleColumn);
        $statusColumn = new ElSwitch('状态', 'status', 1, 0, '有效', '无效');
        $form->addColumn($statusColumn);
        $rule = new Rule('username');
        $requireLaw = new Law('请填写用户账号');
        $phoneLaw = new Law('账号只能是手机号');
        $requireLaw->setRequired(true);
        $rule->addLaw($requireLaw)->addLaw($phoneLaw->setLength(11));
        $form->addRule($rule);

        return ResponseData::success($form->__toArray());
    }
}